package cc.rengu.redp.bizimpl.upmp.controller;


import cc.rengu.redp.bizimpl.upmp.common.BusinessContainer;
import cc.rengu.redp.bizimpl.upmp.domain.CupsInstInfo;
import cc.rengu.redp.common.annotation.Log;
import cc.rengu.redp.common.controller.BaseController;
import cc.rengu.redp.common.domain.QueryRequest;
import cc.rengu.redp.common.domain.RedpResponse;
import cc.rengu.redp.common.exception.RedpException;
import cc.rengu.redp.bizimpl.upmp.domain.AreaInfo;
import cc.rengu.redp.bizimpl.upmp.service.AreaInfoService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import java.util.List;
import java.util.Map;

/**
 * @author LiuYuLiang.LL
 */
@Slf4j
@Validated
@Api(tags = "参数管理:银联地区码管理")
@RestController
@RequestMapping("/admin/merParam/areaInfo")
public class AreaInfoController extends BaseController {
    @Autowired
    private AreaInfoService areaInfoService;

    @GetMapping
//    @RequiresPermissions("areaInfo:view")
    public Map<String, Object> list(QueryRequest request, AreaInfo entity) {
        return getDataTable(this.areaInfoService.findByPage(request, entity));
    }

    @Log("查询单条")
    @GetMapping(value = "/getOne")
    @ApiImplicitParam(paramType = "query", name = "id", value = "id主键", required = true, dataType = "String")
    public AreaInfo getOne(@NotBlank(message = "{required}") String id) {
        return areaInfoService.getById(id);
    }

    @Log("新增")
    @PostMapping
//    @RequiresPermissions("areaInfo:add")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "areaCode", value = "银联地区代码",required = true,  dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "areaName", value = "银联地区名称", required = true, dataType = "String")
    })
    public void add(@Valid AreaInfo entity) throws RedpException {
        try {
            this.areaInfoService.create(entity);
        } catch (Exception e) {
            String message = "新增失败";
            log.error(message, e);
            throw new RedpException(message);
        }
    }

    @Log("修改")
    @PutMapping("/update")
//    @RequiresPermissions("areaInfo:update")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "id", value = "id主键", required = true, dataType = "String")
    })
    public void update(@Valid AreaInfo entity) throws RedpException {
        try {
            this.areaInfoService.update(entity);
        } catch (Exception e) {
            String message = "修改失败";
            log.error(message, e);
            throw new RedpException(message);
        }
    }

    @Log("注销")
    @PutMapping("/cancel")
//    @RequiresPermissions("areaInfo:cancel")
    @ApiImplicitParam(paramType = "query", name = "id", value = "id主键", required = true, dataType = "String")
    public void cancel(@NotBlank(message = "{required}") String id) throws RedpException {
        try {
            this.areaInfoService.cancel(id);
        } catch (Exception e) {
            String message = "注销失败";
            log.error(message, e);
            throw new RedpException(message);
        }
    }

    @Log("删除")
    @DeleteMapping("/{ids}")
//    @RequiresPermissions("areaInfo:delete")
    @ApiImplicitParam(paramType = "path", name = "ids", value = "id主键，逗号隔开", required = true, dataType = "String")
    public void delete(@NotBlank(message = "{required}") @PathVariable String ids) throws RedpException {
        try {
            this.areaInfoService.delete(ids.split(StringPool.COMMA));
        } catch (Exception e) {
            String message = "删除失败";
            log.error(message, e);
            throw new RedpException(message);
        }
    }

    @PostMapping(value = "/check")

    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "id", value = "id主键",  dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "areaCode", value = "银联地区代码",  dataType = "String")
            ,@ApiImplicitParam(paramType = "query", name = "areaName", value = "银联地区名称",  dataType = "String")
    })
    public RedpResponse check(String id, String areaCode, String areaName) throws RedpException {
        LambdaQueryWrapper<AreaInfo> wrapper = new LambdaQueryWrapper();
        if (StringUtils.checkValNotNull(areaCode)) {
            if (StringUtils.checkValNotNull(id)) {
                wrapper.ne(AreaInfo::getId, id);
            }
            wrapper.eq(AreaInfo::getAreaCode, areaCode);
            AreaInfo info = areaInfoService.getOne(wrapper);
            if (null != info) {
                throw new RedpException("[ 银联地区代码 ] 重复，请重新输入");
            }
        }
        if (StringUtils.checkValNotNull(areaName)) {
            wrapper = new LambdaQueryWrapper();
            if (StringUtils.checkValNotNull(id)) {
                wrapper.ne(AreaInfo::getId, id);
            }
            wrapper.likeRight(AreaInfo::getAreaName, areaName);
            AreaInfo info = areaInfoService.getOne(wrapper);
            if (null != info) {
                throw new RedpException("[ 银联地区名称 ] 重复，请重新输入");
            }
        }
        return new RedpResponse().message("验证成功");
    }

    @GetMapping(value = "/areaInfoSelect2List")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "q", value = "银联地区代码|银联地区名称", dataType = "String"),
    })
    public List<Map<String, Object>> select2( String q) {
        LambdaQueryWrapper<AreaInfo> wrapper = new LambdaQueryWrapper();
        if (StringUtils.checkValNotNull(q)) {
            wrapper.and(wp-> wp.likeRight(AreaInfo::getAreaCode, q).
                    or().likeRight(AreaInfo::getAreaName,q));
        }
        wrapper.eq(AreaInfo::getStatus,  BusinessContainer.data_sta_01);
        List<AreaInfo> infos = areaInfoService.list(wrapper);
        List<Map<String, Object>> items = Lists.newArrayList();
        for (AreaInfo info : infos) {
            Map<String, Object> item = Maps.newHashMap();
            item.put("id", info.getAreaCode());
            item.put("name", info.getAreaName());
            items.add(item);
        }
        return items;
    }
    @ApiOperation("省、市级银联区划代码查询")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "q", value = "银联地区代码|银联地区名称", dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "regionLevel",required = true ,value = "行政级别", dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "parentCode", value = "上级行政区划代码", dataType = "String")
    })
    @GetMapping(value = "/areaInfoSelect2")
    public List<Map<String, Object>> agentmanageorgSelect2(String q,@NotBlank(message = "{required}") String regionLevel,String parentCode) {
        return areaInfoService.areaInfoSelect2(q,regionLevel,parentCode);
    }
/*
    @PostMapping("excel")
//    @RequiresPermissions("dict:export")
    public void export(QueryRequest request, AreaInfo entity, HttpServletResponse response) throws RedpException {
        try {
            List<AreaInfo> list = this.areaInfoService.findByPage(request, entity).getRecords();
            ExcelKit.$Export(AreaInfo.class, response).downXlsx(list, false);
        } catch (Exception e) {
            String message = "导出Excel失败";
            log.error(message, e);
            throw new RedpException(message);
        }
    }*/
}
